package org.jaronsource.framework.core.service;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

import org.jaronsource.framework.core.dao.support.Criteria;
import org.jaronsource.framework.core.dao.support.Criteria.ParamEntry;
import org.jaronsource.framework.util.NumberUtils;
import org.jaronsource.framework.util.StringUtils;

public class SearchFormUtils {
	/**
	 * 解释SearchForm生成查询条件
	 * 
	 * @param searchForm
	 * @return
	 */
	public static Criteria parseForm( SearchForm searchForm ) {
		Criteria criteria = new Criteria();
		Map<String, String> form = searchForm.getForm();

		for ( Iterator<Map.Entry<String, String>> iterator = form.entrySet().iterator(); iterator.hasNext(); ) {
			Map.Entry<String, String> entry = iterator.next();
			String name = entry.getKey();
			String value = entry.getValue();

			if ( StringUtils.isBlank( value ) ) continue;

			ParamEntry pe = parseOne( name, value );

			if ( pe != null ) {
				criteria.addEntry( pe );
			}
		}

		return criteria;
	}

	/**
	 * 把Value转换成真实的类型
	 * 
	 * @param fieldName
	 * @param value
	 * @return
	 */
	public static Object converValue( String fieldName, String value ) {

		if ( fieldName.endsWith( SearchForm.TYPE_INT ) ) {
			return NumberUtils.toInt( String.valueOf( value ) );
		}
		else if ( fieldName.endsWith( SearchForm.TYPE_FLOAT ) ) {
			return NumberUtils.toFloat( String.valueOf( value ) );
		}
		else if ( fieldName.endsWith( SearchForm.TYPE_STRING ) ) {
			return String.valueOf( value );
		}
		else if ( fieldName.endsWith( SearchForm.TYPE_SPLITTXT ) ) {
			String[] split = StringUtils.split( value, "," );
			return Arrays.asList( split );
		}
		else if ( fieldName.endsWith( SearchForm.TYPE_SPLIT_INT ) ) {
			String[] split = StringUtils.split( value, "," );
			Integer[] integerArray = NumberUtils.toIntegerArray( split );
			return Arrays.asList( integerArray );
		}
		else if ( fieldName.endsWith( SearchForm.TYPE_SPLIT_FLOAT ) ) {
			String[] split = StringUtils.split( value, "," );
			Float[] floatArray = NumberUtils.toFloatArray( split );
			return Arrays.asList( floatArray );
		}
		return String.valueOf( value );
	}

	/**
	 * 解析一个表达式
	 * 
	 * @param name
	 * @param value
	 * @return
	 * @author mawm at 2013-3-19 下午1:16:12
	 */
	public static ParamEntry parseOne( String name, String value ) {

		if ( StringUtils.isBlank( value ) ) return null;

		ParamEntry pe = null;
		if ( name.contains( SearchForm.OP_GT_SUFFIX ) ) {
			String fieldName = StringUtils.substringBefore( name, SearchForm.OP_GT_SUFFIX );
			Object realValue = converValue( name, value );
			pe = new ParamEntry( fieldName, ">", realValue );
		}
		else if ( name.contains( SearchForm.OP_LT_SUFFIX ) ) {
			String fieldName = StringUtils.substringBefore( name, SearchForm.OP_LT_SUFFIX );
			Object realValue = converValue( name, value );
			pe = new ParamEntry( fieldName, "<", realValue );
		}
		else if ( name.contains( SearchForm.OP_LIKE_SUFFIX ) ) {
			String fieldName = StringUtils.substringBefore( name, SearchForm.OP_LIKE_SUFFIX );
			Object realValue = converValue( name, value );
			if (realValue instanceof String) 
				realValue = realValue + "%";
			pe = new ParamEntry( fieldName, "like", realValue );
		}
		else if ( name.contains( SearchForm.OP_BOTH_LIKE_SUFFIX ) ) {
			String fieldName = StringUtils.substringBefore( name, SearchForm.OP_BOTH_LIKE_SUFFIX );
			Object realValue = converValue( name, value );
			if (realValue instanceof String) 
				realValue = "%" + realValue + "%";
			pe = new ParamEntry( fieldName, "like", realValue );
		}
		else if ( name.contains( SearchForm.OP_NOT_LIKE_SUFFIX ) ) {
			String fieldName = StringUtils.substringBefore( name, SearchForm.OP_NOT_LIKE_SUFFIX );
			Object realValue = converValue( name, value );
			if (realValue instanceof String) 
				realValue = realValue + "%";
			pe = new ParamEntry( fieldName, "not like", realValue );
		}
		else if ( name.contains( SearchForm.OP_NOT_BOTH_LIKE_SUFFIX ) ) {
			String fieldName = StringUtils.substringBefore( name, SearchForm.OP_NOT_BOTH_LIKE_SUFFIX );
			Object realValue = converValue( name, value );
			if (realValue instanceof String) 
				realValue = "%" + realValue + "%";
			pe = new ParamEntry( fieldName, "not like", realValue );
		}
		else if ( name.contains( SearchForm.OP_EQ_SUFFIX ) ) {
			String fieldName = StringUtils.substringBefore( name, SearchForm.OP_EQ_SUFFIX );
			Object realValue = converValue( name, value );
			pe = new ParamEntry( fieldName, "=", realValue );
		}
		else if ( name.contains( SearchForm.OP_NOT_EQ_SUFFIX ) ) {
			String fieldName = StringUtils.substringBefore( name, SearchForm.OP_NOT_EQ_SUFFIX );
			Object realValue = converValue( name, value );
			pe = new ParamEntry( fieldName, "<>", realValue );
		}
		else if ( name.contains( SearchForm.OP_EQ_AND_GT_SUFFIX ) ) {
			String fieldName = StringUtils.substringBefore( name, SearchForm.OP_EQ_AND_GT_SUFFIX );
			Object realValue = converValue( name, value );
			pe = new ParamEntry( fieldName, ">=", realValue );
		}
		else if ( name.contains( SearchForm.OP_EQ_AND_LT_SUFFIX ) ) {
			String fieldName = StringUtils.substringBefore( name, SearchForm.OP_EQ_AND_LT_SUFFIX );
			Object realValue = converValue( name, value );
			pe = new ParamEntry( fieldName, "<=", realValue );
		}
		else if ( name.contains( SearchForm.OP_IN_SUFFIX ) ) {
			String fieldName = StringUtils.substringBefore( name, SearchForm.OP_IN_SUFFIX );
			Object realValue = converValue( name, value );
			pe = new ParamEntry( fieldName, Criteria.Op.IN, realValue );
		}
		else {
			return null;
		}
		return pe;

	}
}
